package ru.arriah.servicedesk.web.admin.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.EmployeeBean;
import ru.arriah.servicedesk.bean.EmployeeTypeBean;
import ru.arriah.servicedesk.bean.GroupBean;
import ru.arriah.servicedesk.ejb.EmployeeManagerLocalHome;
import ru.arriah.servicedesk.ejb.EmployeeManagerLocalObject;
import ru.arriah.servicedesk.ejb.GroupManagerLocalHome;
import ru.arriah.servicedesk.ejb.GroupManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class DisplayEmployeeFormAction extends DumbAction {
   protected EmployeeManagerLocalHome employeeManagerHome;
   protected GroupManagerLocalHome groupManagerHome;  
   
   public DisplayEmployeeFormAction() throws NamingException {
      super();
      employeeManagerHome =
         Utils.getEmployeeManagerHomeInterface();
      
      groupManagerHome =
        Utils.getGroupManagerHomeInterface();
   }

   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      try {
        
        EmployeeManagerLocalObject employeeManager = 
          employeeManagerHome.create();
        
        GroupManagerLocalObject groupManager =
          groupManagerHome.create();
        
        Collection<GroupBean> groupsList = 
          groupManager.selectGroups();
                  
        Collection<EmployeeTypeBean> typesList = 
          employeeManager.selectEmployeeTypes();
        
        int employeeId = 
          idVal(request.getParameter("employee_id"));        
        
        if(employeeId != 0) {          
          EmployeeBean employeeBean = 
            employeeManager.selectEmployee(employeeId);
          
          request.setAttribute("employeeBean", employeeBean);                   
        }                       
        
        employeeManager.remove();
        groupManager.remove();                
        
        request.setAttribute("typesList", typesList);
        request.setAttribute("groupsList", groupsList);
        
        return actionMapping.getSuccessTarget();
      } catch (Exception e) {
         handleException(e, "execute");
         throw new InternalException("ErrorMessage.internal");
      }
   }
}